package com.alei.redis.redisdemo.controller;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

/**
 * @author ShiLei
 * @version v1.0
 * @date 2020-06-30 10:57
 */
public class PipelineJedis {


    /**
     * 不使用Redis的pipeline功能设置一万条数据花费时间:123094
     * 使用Redis的pipeline功能设置一万条数据花费时间:1325
     */
    public static void main(String[] args) {
        Jedis jedis = new Jedis("47.95.223.193", 6382);
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            jedis.hset("keyPipeline:" + i, "field:" + i, "value:" + i);
        }
        long end = System.currentTimeMillis();
        System.out.println("不使用Redis的pipeline功能设置一万条数据花费时间:" + (end - begin));

        System.out.println("使用 pipeline 功能 ===========================");

        long begin1 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            Pipeline pipeline = jedis.pipelined();
            for (int j = i * 100; j < (i + 1) * 100; j++) {
                pipeline.hset("pipeline:" + i, "field:" + i, "value:" + i);
            }
            pipeline.syncAndReturnAll();
        }
        long end1 = System.currentTimeMillis();
        System.out.println("使用Redis的pipeline功能设置一万条数据花费时间:" + (end1 - begin1));
    }
}
